﻿using BLL;
using DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admincp_Origins : System.Web.UI.Page
{
    DigitalWorldDataContext db = new DigitalWorldDataContext();
    OriginsBLL o = new OriginsBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.loadData();
            rbtnStatus.Enabled = false;
            btnUpdate.Enabled = false;
        }
    }

    private void loadData()
    {
        gvOrigins.DataSource = o.getOrigin();
        gvOrigins.DataBind();
    }
    public void reset() {
        txtOriginID.Text = String.Empty;
        txtCountryName.Text = String.Empty;
        rbtnStatus.Enabled = false;
        btnUpdate.Enabled = false;
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        origin or = new origin { 
            originid=txtOriginID.Text,
            countryname=txtCountryName.Text
        };
        o.Origin_Insert(or);
        this.loadData();
        this.reset();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        bool status;
        if (rbtnStatus.SelectedItem.Value == "1")
        {
            status = true;
        }
        else
        {
            status = false;
        }
        origin or = new origin{
            originid = txtOriginID.Text,
            countryname = txtCountryName.Text,
            status = status
        };
        o.Origin_Update(or);
        this.loadData();
        this.reset();
    }
    protected void gvOrigins_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        txtOriginID.Text = Server.HtmlDecode(gvOrigins.Rows[e.NewSelectedIndex].Cells[0].Text);
        txtCountryName.Text = Server.HtmlDecode(gvOrigins.Rows[e.NewSelectedIndex].Cells[1].Text);
        rbtnStatus.Enabled = true;
        btnUpdate.Enabled = true;
    }
    protected void gvOrigins_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvOrigins.PageIndex = e.NewPageIndex;
        this.loadData();
    }
    protected void gvOrigins_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string originid = gvOrigins.Rows[e.RowIndex].Cells[0].Text;
        o.Origin_Delete(originid);
        this.loadData();
    }
}